package com.txzt.gaea.business.device.service.impl;

import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeviceInfoServiceImpl extends BaseServiceImpl<DeviceInfo> implements IDeviceInfoService {


    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo) {
        if (StringUtils.isNotEmpty(deviceInfo.getDeviceName())) {
            return sqlManager.query(DeviceInfo.class)
                    .andEq("device_type",deviceInfo.getDeviceType())
                    .andLike("device_name",deviceInfo.getDeviceName())
                    .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                    .orderBy("id desc")
                    .select();
        }else {
            return sqlManager.query(DeviceInfo.class)
                    .andEq("device_type",deviceInfo.getDeviceType())
                    .andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode())
                    .orderBy("id desc")
                    .select();
        }
    }

    @Override
    public int addDeviceInfo(DeviceInfo deviceInfo) {
        DeviceInfo res1 = sqlManager.query(DeviceInfo.class)
                .andEq("device_code",deviceInfo.getDeviceCode())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode())
                .single();
        if (null != res1) {
            return 98;
        }
        DeviceInfo res2 = sqlManager.query(DeviceInfo.class)
                .andEq("device_type",deviceInfo.getDeviceType())
                .andEq("device_name",deviceInfo.getDeviceName())
                .andEq("device_brand",deviceInfo.getDeviceBrand())
                .andEq("device_model",deviceInfo.getDeviceModel())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode())
                .single();
        if (null != res2) {
            return 99;
        }
        deviceInfo.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        deviceInfo.setAvailableQty(deviceInfo.getInventoryQty());
        return sqlManager.insertTemplate(deviceInfo);
    }

    @Override
    public int editDeviceInfo(DeviceInfo deviceInfo) {
        DeviceInfo res1 = sqlManager.query(DeviceInfo.class)
                .andEq("device_code",deviceInfo.getDeviceCode())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode())
                .andNotEq("id",deviceInfo.getId())
                .single();
        if (null != res1) {
            return 98;
        }
        DeviceInfo res2 = sqlManager.query(DeviceInfo.class)
                .andEq("device_type",deviceInfo.getDeviceType())
                .andEq("device_name",deviceInfo.getDeviceName())
                .andEq("device_brand",deviceInfo.getDeviceBrand())
                .andEq("device_model",deviceInfo.getDeviceModel())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode())
                .andNotEq("id",deviceInfo.getId())
                .single();
        if (null != res2) {
            return 99;
        }
        return sqlManager.updateTemplateById(deviceInfo);
    }
}
